package com.zzedu.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.zzedu.common.cache.CacheNameContansts;
import com.zzedu.common.enums.CommonEnums;
import com.zzedu.common.utils.StringUtils;
import com.zzedu.common.utils.bean.BeanUtils;
import com.zzedu.domain.ZzCourse;
import com.zzedu.domain.ZzPictureBook;
import com.zzedu.domain.ZzUploadFile;
import com.zzedu.dto.ZzCourseDTO;
import com.zzedu.integration.oss.IOssClientService;
import com.zzedu.mapper.ZzCourseMapper;
import com.zzedu.mapper.ZzPictureBookMapper;
import com.zzedu.service.IZzCourseService;
import com.zzedu.service.IZzUploadFileService;
import com.zzedu.vo.StuLearnReportDataVO;
import com.zzedu.vo.ZzCourseVO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 课程包管理Service业务层处理
 *
 * @author Mr.Wang
 * @date 2025-02-04
 */
@Service
public class ZzCourseServiceImpl extends ServiceImpl<ZzCourseMapper, ZzCourse> implements IZzCourseService {


    @Autowired
    IZzUploadFileService zzUploadFileService;

    @Autowired
    IOssClientService ossClientService;

    @Autowired
    ZzPictureBookMapper zzPictureBookMapper;

    @Override
    @Cacheable(value = CacheNameContansts.COURSE_CACHE, key = "'course:'+#p0")
    public ZzCourse getById(Serializable id) {
        return super.getById(id);
    }

    @Override
    public boolean save(ZzCourse entity) {
        return super.save(entity);
    }

    @Override
    @CacheEvict(value = CacheNameContansts.COURSE_CACHE, key = "'course:'+#p0.id")
    public boolean updateById(ZzCourse entity) {
        return super.updateById(entity);
    }

    @Override
    public LambdaQueryWrapper<ZzCourse> buildQuery(ZzCourseDTO zzCourse) {
        LambdaQueryWrapper<ZzCourse> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.like(StringUtils.isNotBlank(zzCourse.getName()), ZzCourse::getName, zzCourse.getName());
        lambdaQueryWrapper.eq(null != zzCourse.getState(), ZzCourse::getState, zzCourse.getState());
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(zzCourse.getLevel()), ZzCourse::getLevel, zzCourse.getLevel());
        lambdaQueryWrapper.orderByAsc(ZzCourse::getSortNo).orderByDesc(ZzCourse::getId);
        return lambdaQueryWrapper;
    }

    @Override
    @Cacheable(value = CacheNameContansts.COURSE_CACHE, key = "'calc:course:'+#p0")
    public void getWordsAndSentences(StuLearnReportDataVO stuLearnReportDataVO,Long id) {
        List<ZzPictureBook> zzPictureBookList = zzPictureBookMapper.selectList(Wrappers.lambdaQuery(ZzPictureBook.class).eq(ZzPictureBook::getUnitId, id));
        Set<String> words = Sets.newHashSet();
        Set<String> sentences = Sets.newHashSet();
        if (CollectionUtils.isNotEmpty(zzPictureBookList)) {
            for (ZzPictureBook zzPictureBook : zzPictureBookList) {
                if (StringUtils.isNotBlank(zzPictureBook.getWords())) {
                    words.addAll(JSON.parseArray(zzPictureBook.getWords(), String.class));
                }
                if (StringUtils.isNotBlank(zzPictureBook.getSentence())) {
                    sentences.addAll(JSON.parseArray(zzPictureBook.getSentence(), String.class));
                }
            }
        }
        stuLearnReportDataVO.setWords(words);
        stuLearnReportDataVO.setSentences(sentences);
    }

    @Override
    public List<ZzCourseVO> convertVO(List<ZzCourse> zzCourseList) {
        return Optional.ofNullable(zzCourseList).orElse(Lists.newArrayList()).stream().map(zzCourse -> {
            ZzCourseVO zzCourseVO = new ZzCourseVO();
            BeanUtils.copyBeanProp(zzCourseVO, zzCourse);
            if (null != zzCourse.getAppBackgroundImage()) {
                ZzUploadFile zzUploadFile = this.zzUploadFileService.getById(zzCourse.getAppBackgroundImage());
                if (null != zzUploadFile) {
                    zzCourseVO.setAppBackgroundImageURL(ossClientService.convertURL(zzUploadFile.getUrl()));
                }
            }
            if (null != zzCourse.getPcBackgroundImage()) {
                ZzUploadFile zzUploadFile = this.zzUploadFileService.getById(zzCourse.getPcBackgroundImage());
                if (null != zzUploadFile) {
                    zzCourseVO.setPcBackgroundImageURL(ossClientService.convertURL(zzUploadFile.getUrl()));
                }
            }
            return zzCourseVO;
        }).collect(Collectors.toList());
    }

    @Override
    public List<ZzCourse> getStuCourseListNews(Long stuId, Long schoolId, String level,List<Integer> stateList) {
        return baseMapper.getStuCourseListNews(stuId, schoolId, level,stateList);
    }
}